package com.aiju.cash.ui.pay;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.PayApi;
import com.aiju.cash.ui.billing.BillingActivity;
import com.aiju.cash.ui.pay.entity.Pay;
import com.aiju.cash.ui.pay.manager.PayManager;
import com.aiju.utils.L;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.SimpleDialog;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-05 16:14
 * @version: V1.0
 * @Description: 台卡支付
 */
public class PayQRCodeActivity extends BaseTitleActivity {

    public static final int SHOW_QR = 2;
    public static final int SHOW_ZHI = 0;
    public static final int SHOW_WECHAT = 1;

    private Button mButton;
    private ImageView mImageView;

    private int type;
    private String price;
    private ImageView payTypeImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);

        setRightTitle("取消收款");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SimpleDialog(PayQRCodeActivity.this)
                        .setTitileText("是否取消收款")
                        .setLeftText("否")
                        .setRightText("是")
                        .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit() {
                                finish();
                            }
                        }).setLayoutParams().show();
            }
        });
        mImageView = (ImageView) findViewById(R.id.iv_pay_qr);
        mButton = (Button) findViewById(R.id.btn_pay);
        type = getIntent().getIntExtra("type", -1);
        if (type == SHOW_QR) {
            setCenterTitle("店铺收款");
            findViewById(R.id.view_qr_25).setVisibility(View.VISIBLE);
            mButton.setText("支付完成");
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new SimpleDialog(PayQRCodeActivity.this)
                            .setTitileText("确定已完成收款")
                            .setLeftText("尚未完成")
                            .setRightText("确定完成")
                            .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                                @Override
                                public void onCancel() {
                                }

                                @Override
                                public void onSubmit() {

                                    starPaySussecc();
                                }
                            }).setLayoutParams().show();
                }
            });
            getQRString();
            if (!mUser.getShop().isWx()) {
                findViewById(R.id.iv_qr_wechat).setVisibility(View.GONE);
                findViewById(R.id.view_qr_25).setVisibility(View.GONE);
            }
            if (!mUser.getShop().isAlipay()) {
                findViewById(R.id.iv_pay_type).setVisibility(View.GONE);
                findViewById(R.id.view_qr_25).setVisibility(View.GONE);
            }
        } else {
            setCenterTitle("二维码收款");
            findViewById(R.id.iv_qr_wechat).setVisibility(View.GONE);
            mButton.setText("转换为扫一扫收款");
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startPayZhiFBaoActivity(type);
                    finish();
                }
            });
            payTypeImageView = (ImageView) findViewById(R.id.iv_pay_type);
            if (type == 0) {
                payTypeImageView.setImageResource(R.mipmap.icon_pay_zhifubao);
            } else if (type == 1) {
                payTypeImageView.setImageResource(R.mipmap.icon_pay_wechat);
            }
            getZhifu();
        }
    }

    private void startPayZhiFBaoActivity(int type) {
        Intent zhifIntent = new Intent(PayQRCodeActivity.this, PayZhiFBaoActivity.class);
        Bundle zhifBundle = new Bundle();
        zhifBundle.putSerializable("pay", pay);
        zhifBundle.putInt("type", type);
        zhifIntent.putExtras(zhifBundle);
        startActivity(zhifIntent);
    }

    private void starPaySussecc() {
        if (PayManager.isOrderFrom()) {
            Intent intent = new Intent(this, BillingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, PayActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }

    private Pay pay;
    /**
     * 扫码
     */
    private void getZhifu() {
        pay = (Pay) getIntent().getExtras().getSerializable("pay");
        price = pay.getPrice();
        ((TextView) findViewById(R.id.tv_qr_money)).setText(price);
        mDialogHelper.showLoadingProgressDialog();
        new PayApi().qrCodePay(type, pay.getStoreId(), pay.getSubject(), pay.getPrice(),
                pay.getSumPrice(), mUser.getOperatorId(), mUser.getUserName(), pay.getPrivType(), pay.getPrivPrice(),
                pay.getShouldPrice(), pay.getActualPrice(), pay.getChangePrice(),
                pay.getRoundPrice(), pay.getDetail(), new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        T.ToastNetError();
                        mDialogHelper.dismissProgressDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson json = new DataJson(response);
                        if (json.isSuccess()) {
                            Bitmap mBitmap = CodeUtils.createImage(json.getData().toString(), 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.icon_launcher));
                            mImageView.setImageBitmap(mBitmap);
                        } else {
                            T.ToastShort(json.getMsg());
                        }

                    }
                });
    }

    /**
     * 台卡
     */
    private void getQRString() {
        price = getIntent().getStringExtra("price");
        ((TextView) findViewById(R.id.tv_qr_money)).setText(price);

        StringBuilder sb = new StringBuilder("https://trade.ecbao.cn" + "/code/showCode?storeId=");
        sb.append(mUser.getShop().getStoreId());
        sb.append("&shopName=");
        sb.append(mUser.getShop().getShopName());
   /*     sb.append("&token=");
        sb.append(mUser.getToken());*/

        L.d(sb.toString());
        Bitmap mBitmap = CodeUtils.createImage(sb.toString(), 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.icon_launcher));
        mImageView.setImageBitmap(mBitmap);
    }

}
